package com.zzyl.nursing.controller.member;

import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.service.MemberReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/member/reservation")
@Api(tags =  "预约信息相关接口")
public class MemberReservationController extends BaseController {

    @Autowired
    private IFamilyMemberService familyMemberService;

    @Autowired
    IReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        Integer cancelledReservationCount = familyMemberService.getCancelledReservationCount(userId);
        System.out.println("------" + userId);
        return R.ok(cancelledReservationCount);
    }

    @GetMapping("/countByTime")
    @ApiOperation("查询每个时间段剩余的预约数量")
    public R<Integer> getCountByTime(Long time) {
        familyMemberService.selectRestReservationCount(time);
        return R.ok();
    }

    /**
     * 新增预约信息
     */

    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增预约信息")
    public AjaxResult add(@ApiParam(value = "预约信息实体", required = true) @RequestBody Reservation reservation)
    {
        return toAjax(reservationService.insertReservation(reservation));
    }

    @GetMapping("page")
    @ApiOperation("分页查询预约信息")
    public R<TableDataInfo> page(Reservation reservation) {
        startPage();
        TableDataInfo dataTable = getDataTable(reservationService.selectReservationList(reservation));
        return R.ok(dataTable);
    }
    @ApiOperation("取消预约")
    @PutMapping("/{id}/cancel")
    public R cancel(@PathVariable Long id) {
        reservationService.updateCancelReservation(id);
        return R.ok();
    }
}
